import 'dart:typed_data';

extension MyUint8ListExtension on Uint8List {
  String toHexString({bool showIndex = false, bool showLength = false, bool delimiter = false}) {
    final StringBuffer buffer = StringBuffer();

    if (showLength) {
      buffer.write("$length [");
    } else {
      buffer.write("[");
    }

    final String sep = delimiter ? ", " : " ";
    final int lastIndex = length - 1;

    for (int i = 0; i < length; i++) {
      final String hexValue = this[i].toRadixString(16).padLeft(2, '0').toUpperCase();
      if (showIndex) {
        buffer.write("($i)");
      }
      buffer.write(hexValue);
      if (i < lastIndex) {
        buffer.write(sep);
      }
    }

    buffer.write("]");
    return buffer.toString();
  }

  String toMacAddressString() {
    final StringBuffer buffer = StringBuffer();
    for (int i = 0; i < length; i++) {
      final String hexValue = this[i].toRadixString(16).padLeft(2, '0').toUpperCase();
      buffer.write(hexValue);
      if (i < length - 1) {
        buffer.write(":");
      }
    }

    return buffer.toString();
  }

  bool isEquals(Uint8List other) {
    if (length != other.length) {
      return false;
    }
    for (int i = 0; i < length; i++) {
      if (this[i] != other[i]) {
        return false;
      }
    }
    return true;
  }
}
